---@coding utf8

local dgetinfo = debug.getinfo
local dtraceback= debug.traceback
local smatch = string.match

---@brief 获取当前文件路径,当前获取到的并不是绝对路径
---@param level (nil|integer) 堆栈层级：默认1标识当前帧，每+1标识更上一层栈帧
---@return string
function get_current_file_dir(level)
    if nil == level then
        level = 1
    end
    local dinfo = dgetinfo(level, "S")
    --print(dinfo.short_src, dinfo.source)
    local file_path = smatch(dinfo.source, "@?(.*[/\\])")
    return file_path
end

---@brief 获取当前函数调用栈
---@param level (nil|integer) 堆栈层级：默认1标识当前帧，每+1标识更上一层栈帧
---@return string
function get_current_call_stack(level)
    if nil == level then
        level = 1
    end
    local strack_str = dtraceback(level)
    return strack_str
end

---@brief 获取当前函数名
---@param level integer
---@return string
function get_current_function_name(level)
    if nil == level then
        level = 1
    end
    local dinfo = dgetinfo(level, "n")
    return dinfo.name
end

---@breif 获取当前文件信息：文件名(可能包含@),行号
---@param level integer
---@return string
---@return integer
function get_current_fileinfo(level)
    if nil == level then
        level = 1
    end
    local dinfo = dgetinfo(level, "Sln")
    return dinfo.source, dinfo.currentline
end



return {
    get_current_file_dir = get_current_file_dir,
    get_current_call_stack = get_current_call_stack,
    get_current_function_name = get_current_function_name,
    get_current_fileinfo = get_current_fileinfo,
}